package com.filldream.fastboot.core.job.task;

import com.alibaba.fastjson2.JSONObject;
import com.filldream.fastboot.common.constant.FinalStr;
import com.filldream.fastboot.common.okutil.RobotUtil;
import com.filldream.fastboot.common.okutil.cls.dto.ClsNewsSimpleItem;
import com.filldream.fastboot.common.okutil.stockIndex.StockIndexUtil;
import com.filldream.fastboot.core.cache.CacheMap;
import com.filldream.fastboot.core.cache.CacheObj;
import com.filldream.fastboot.core.cache.CacheUtil;
import com.filldream.fastboot.core.service.websocket.ClsNewsServer;
import com.filldream.fastboot.core.thread.ThreadPoolUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Component("StockIndexTask")
@RequiredArgsConstructor
public class StockIndexTask {

    /**
     * 自动发送离岸人民币汇率消息
     * 顺带发送美元指数
     */
    public static void autoSendStockIndex(){
        List<ClsNewsSimpleItem> addList = new ArrayList<>();
        //离岸人民币汇率
        JSONObject usdcnh = StockIndexUtil.getDiyHL("USDCNH");
        if (usdcnh != null) {
            String price = usdcnh.getString("price");
            String increase = usdcnh.getString("increase");
            BigDecimal increaseMoney = new BigDecimal(increase).abs();
            //保留两位小数的价格
            BigDecimal priceMoney = new BigDecimal(price).setScale(2, BigDecimal.ROUND_DOWN);
            //封装人民币汇率消息
            ClsNewsSimpleItem rmbItem = new ClsNewsSimpleItem();
            rmbItem.setLinkUrl("https://gushitong.baidu.com/foreign/global-USDCNH");
            rmbItem.setTitle("离岸人民币: " + priceMoney );
            rmbItem.setIsTop( priceMoney.compareTo(new BigDecimal("7.3")) >=0  );
            addList.add(rmbItem);

            //封装美元指数消息
            JSONObject diniw = StockIndexUtil.getDiyHL("DINIW");
            BigDecimal diniwMoney;
            if( diniw != null ) {
                diniwMoney = new BigDecimal(diniw.getString("price")).setScale(2, BigDecimal.ROUND_DOWN);
                ClsNewsSimpleItem DINIWItem = new ClsNewsSimpleItem();
                DINIWItem.setLinkUrl("https://gushitong.baidu.com/foreign/global-DINIW");
                DINIWItem.setTitle("美元指数: " + diniwMoney );
                DINIWItem.setIsTop( diniwMoney.compareTo(new BigDecimal("106.00")) >=0  );
                addList.add(DINIWItem);
            }

            //封装美元黄金消息
            ClsNewsSimpleItem USAUItem = new ClsNewsSimpleItem();
            USAUItem.setLinkUrl("https://gushitong.baidu.com/stock/us-USAU?name=%25E7%25BE%258E%25E5%259B%25BD%25E9%25BB%2584%25E9%2587%2591");
            String usua = StockIndexUtil.getUsua();
            USAUItem.setTitle("美元黄金: " + usua );
            USAUItem.setIsTop( new BigDecimal(usua).compareTo(new BigDecimal("8")) >=0  );
            addList.add(USAUItem);

            //封装美国十年期国债收益率
            ClsNewsSimpleItem US10YItem = new ClsNewsSimpleItem();
            US10YItem.setLinkUrl("https://wallstreetcn.com/markets/codes/US10YR.OTC");
            BigDecimal us10YMoney = StockIndexUtil.getUS10Y();
            US10YItem.setTitle("美国十年期国债收益率: " + us10YMoney + "%" );
            US10YItem.setIsTop( us10YMoney.compareTo(new BigDecimal("4.4")) >=0  );
            addList.add(US10YItem);

            //封装中国三十年期国债收益率
            ClsNewsSimpleItem CN30YItem = new ClsNewsSimpleItem();
            CN30YItem.setLinkUrl("https://wallstreetcn.com/markets/codes/CN30YR.OTC");
            BigDecimal cn30YMoney = StockIndexUtil.getCN30Y();
            CN30YItem.setTitle("中国三十年期国债收益率: " + cn30YMoney + "%" );
            CN30YItem.setIsTop( cn30YMoney.compareTo(new BigDecimal("5.0")) <= 0  );
            addList.add(CN30YItem);

            //封装息差
            if( us10YMoney != null && cn30YMoney != null ) {
                ClsNewsSimpleItem xcItem = new ClsNewsSimpleItem();
                xcItem.setLinkUrl("https://wallstreetcn.com/markets/codes/CN30YR.OTC");
                BigDecimal xc = us10YMoney.subtract(cn30YMoney);
                xcItem.setTitle("国债息差率: " + xc + "%" );
                //xcItem.setIsTop( xc.compareTo(new BigDecimal("2.2")) >=0  );
                addList.add(xcItem);
            }

            //封装两市市值
            ClsNewsSimpleItem aMarketPriceItem = new ClsNewsSimpleItem();
            aMarketPriceItem.setLinkUrl("https://stockpage.10jqka.com.cn/1A0001/");
            BigDecimal sz = StockIndexUtil.getAMarketPrice();
            aMarketPriceItem.setTitle("两市市值: " + sz +"万亿" );
            aMarketPriceItem.setIsTop( sz.compareTo(new BigDecimal("115")) >=0  );
            addList.add(aMarketPriceItem);

            //封装A50期指当月连续
            JSONObject jsonObject = StockIndexUtil.getA50KeepDate();
            if( jsonObject != null ) {
                BigDecimal a50Price = jsonObject.getBigDecimal("f43").divide(new BigDecimal("10"),1,BigDecimal.ROUND_DOWN);
                BigDecimal a50Zdf = jsonObject.getBigDecimal("f170").divide(new BigDecimal("100"),2,BigDecimal.ROUND_DOWN);
                ClsNewsSimpleItem a50Item = new ClsNewsSimpleItem();
                a50Item.setLinkUrl("https://quote.eastmoney.com/q/104.cn00y.html");
                a50Item.setTitle("A50期指当月连续: " + a50Price + " " + a50Zdf + "%" );
                a50Item.setIsTop( a50Zdf.compareTo(BigDecimal.ZERO) < 0  );
                addList.add(a50Item);
            }

            //发送机器人消息
            ThreadPoolUtil.J.getExecutor().execute( () -> {
                Map<String, String> btnMap = new HashMap<>();
                String refreshUrl = "http://" + (FinalStr.getActive().equals("dev") ? "localhost" : "118.195.133.175") +":8110/common/stock/refreshStockIndex";
                btnMap.put("再次获取", refreshUrl);
                RobotUtil.autoDD(addList,btnMap);
                RobotUtil.autoFs(addList);
            });

            //推送消息到ws
            ThreadPoolUtil.J.getExecutor().execute(() -> {
                new ClsNewsServer().sendNewsList(addList);
            });
        }
    }

}